package chat.network.http;

import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.whu.administrator.ws.MyApplication;
import com.whu.administrator.ws.R;
import com.whu.administrator.ws.VolleySingleton;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import inter.Delegate;

/**获取消息
 * Created by 凯 on 2015/10/23.
 */
public class GetUserMessage {

    Delegate delegate;
    String user_FromID;
    String user_ToID;

    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }

    public void setUser_FromID(String user_FromID) {
        this.user_FromID = user_FromID;
    }

    public void setUser_ToID(String user_ToID) {
        this.user_ToID = user_ToID;
    }

    public GetUserMessage() {
    }

    public void get()
    {
        String IP = MyApplication.share().getResources().getString(R.string.IP);
        String url ="http://"+IP+"/Chat/GetUserMessages";
        StringRequest stringRequest = new StringRequest(Request.Method.POST,url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        if (delegate != null)
                            delegate.onResponse(response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (delegate != null)
                    delegate.onErrorResponse(error);
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map map = new HashMap();
                map.put("user_FromID",user_FromID);
                map.put("user_ToID",user_ToID);
                return  map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data,"utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }


    public static class UserMsgs {
        private String user_FromID;
        private String user_FromUserName;
        private String message_Content;
        private String message_Time;
        private String message_Type;

        public String getUser_FromID() {
            return user_FromID;
        }
        public void setUser_FromID(String user_FromID) {
            this.user_FromID = user_FromID;
        }
        public String getUser_FromUserName() {
            return user_FromUserName;
        }
        public void setUser_FromUserName(String user_FromUserName) {
            this.user_FromUserName = user_FromUserName;
        }
        public String getMessage_Content() {
            return message_Content;
        }
        public void setMessage_Content(String message_Content) {
            this.message_Content = message_Content;
        }
        public String getMessage_Time() {
            return message_Time;
        }
        public void setMessage_Time(String message_Time) {
            this.message_Time = message_Time;
        }
        public String getMessage_Type() {
            return message_Type;
        }
        public void setMessage_Type(String message_Type) {
            this.message_Type = message_Type;
        }
    }


    public static class ResponseUserMessages {
        private String status;
        private int unRead_Num;

        private List<UserMsgs> userMessages_List;

        public String getStatus() {
            return status;
        }
        public void setStatus(String status) {
            this.status = status;
        }
        public int getUnRead_Num() {
            return unRead_Num;
        }
        public void setUnRead_Num(int unRead_Num) {
            this.unRead_Num = unRead_Num;
        }
        public List<UserMsgs> getUserMessages_List() {
            return userMessages_List;
        }
        public void setUserMessages_List(List<UserMsgs> userMessages_List) {
            this.userMessages_List = userMessages_List;
        }
    }

}
